/**
 * Created by jiachneyu on 2018/4/21.
 */
const blog = require('./blog');
const findAndUpdate = blog.findAndUpdate;
const findOne = blog.findOne;
const upsert = blog.update;
const db = require("../../db");
const collection = db.get("discuss");

const getArticle = async (_id, isPv) => {
    console.log(isPv);
    if (isPv == 1) {
        return await findAndUpdate({_id: _id}, {$inc: {pv: 1}});
    }
    else {
        return await findOne({_id: _id});
    }
}

const getArticleName = async (_id) => {
    return await  findOne({_id: _id}, {title: 1})
}

const like = async (body) => {
    return await findAndUpdate({_id: body._id}, {$inc: {like: body.like}})
}

const addIp = (_id, ip) => {
    return upsert()
}

const addDiscuss = async (body) => {
    return await collection.insert(body).then(res => {
        if (res) {
            db.close();
            return {
                code: 1,
                data: res
            }
        }
    }).catch(err => {
        console.log(err);
        db.close();
        return {
            code: -1
        }
    })
}

const replayDiscuss = async (body) => {
    const chatData = {
        toName: body.toName,
        replayName: body.username,
        content: body.content,
        replayTime: body.chatTime,
        // email:
    }
    console.log(chatData)
    return await collection.update({_id: body._id}, {"$push": {chat: chatData}}).then(res => {
        if (res) {
            console.log(res)
            return {
                code: 1,
            }
        }
    }).catch(err => {
        console.log(err);
        return {
            code: -1
        }
    })
}

const getDiscuss = async (body) => {
    return await collection.find({blog: body}).then(res => {
        if (res) {
            db.close();
            return {
                code: 1,
                data: res
            }
        }
    }).catch(err => {
        console.log(err);
        db.close();
        return {
            code: -1
        }
    })
}

const getReplyEmail = async body => {
    return await collection.findOne({
        _id: body._id,
        blog: body.blog,
        "chat.toName": body.toName
    }, {email: 1}).then(async res => {
        if (res && res.email) {
            return {
                code: 1,
                data: res.email
            }
        }
        else {
            return await collection.findOne({
                _id: body._id,
                blog: body.blog,
                username: body.toName
            },{email: 1}).then(val=>{
                if(val && val.email){
                    return {
                        code: 1,
                        data: val.email
                    }
                }
            })
        }
    }).catch(err => {
        console.log(err);
        db.close();
        return {
            code: -1
        }
    })
}

module.exports = {
    getArticle: getArticle,
    addIp: addIp,
    like,
    addDiscuss,
    getDiscuss,
    replayDiscuss,
    getArticleName,
    getReplyEmail
};